def inf(support,im_types,ex_types,SC_types): pass #plugin information
def impt(imfl,imSC,debug): pass #import function
def expt(exfl,exSC,debug): pass #export function
#^functions are required for extension modules, but don't have to contain any info

#--<<imports>>--
import Cmn as C

#GCN/Wii common formats and functions

def setFile(F):
    C.setFile(F)
    global f
    f = F

#Reading data:

def CP_VAT_REG_A(UVAT): #Group 0
    #0000 0000 0000 0000 0000 0000 0000 0001   - Pos count
    #0000 0000 0000 0000 0000 0000 0000 1110   - Pos format
    #0000 0000 0000 0000 0000 0001 1111 0000   - Pos shift
    #0000 0000 0000 0000 0000 0010 0000 0000   - Normal count
    #0000 0000 0000 0000 0001 1100 0000 0000   - Normal format
    #0000 0000 0000 0000 0010 0000 0000 0000   - Color0 count
    #0000 0000 0000 0001 1100 0000 0000 0000   - Color0 format
    #0000 0000 0000 0010 0000 0000 0000 0000   - Color1 count
    #0000 0000 0001 1100 0000 0000 0000 0000   - Color1 format
    #0000 0000 0010 0000 0000 0000 0000 0000   - Tex0 coord count
    #0000 0001 1100 0000 0000 0000 0000 0000   - Tex0 coord format
    #0011 1110 0000 0000 0000 0000 0000 0000   - Tex0 coord shift
    #0100 0000 0000 0000 0000 0000 0000 0000   - Byte dequant shift
    #1000 0000 0000 0000 0000 0000 0000 0000   - Normal index 3
    return [(lo & 1), ((lo >> 1) & 7), ((lo >> 4) & 31), ((lo >> 9) & 1), ((lo >> 10) & 7),
            ((lo >> 13) & 1), ((lo >> 14) & 7), ((lo >> 17) & 1), ((lo >> 18) & 7),
            ((lo >> 21) & 1), ((lo >> 22) & 7), ((lo >> 25) & 31), ((lo >> 30) & 1), ((lo >> 31) & 1)]

def CP_VAT_REG_B(UVAT): #Group 1
    #0000 0000 0000 0000 0000 0000 0000 0001   - Tex1 coord elements
    #0000 0000 0000 0000 0000 0000 0000 1110   - Tex1 coord format
    #0000 0000 0000 0000 0000 0001 1111 0000   - Tex1 coord frac
    #0000 0000 0000 0000 0000 0010 0000 0000   - Tex2 coord elements
    #0000 0000 0000 0000 0001 1100 0000 0000   - Tex2 coord format
    #0000 0000 0000 0011 1110 0000 0000 0000   - Tex2 coord frac
    #0000 0000 0000 0100 0000 0000 0000 0000   - Tex3 coord elements
    #0000 0000 0011 1000 0000 0000 0000 0000   - Tex3 coord format
    #0000 0111 1100 0000 0000 0000 0000 0000   - Tex3 coord frac
    #0000 1000 0000 0000 0000 0000 0000 0000   - Tex4 coord elements
    #0111 0000 0000 0000 0000 0000 0000 0000   - Tex4 coord format
    return [(lo & 1), ((lo >> 1) & 7), ((lo >> 4) & 31), ((lo >> 9) & 1), ((lo >> 10) & 7), ((lo >> 13) & 31),
            ((lo >> 18) & 1), ((lo >> 19) & 7), ((lo >> 22) & 31), ((lo >> 27) & 1), ((lo >> 28) & 7)]

def CP_VAT_REG_C(UVAT): #Group 2
    #0000 0000 0000 0000 0000 0000 0001 1111   - Tex4 coord frac
    #0000 0000 0000 0000 0000 0000 0010 0000   - Tex5 coord elements
    #0000 0000 0000 0000 0000 0001 1100 0000   - Tex5 coord format
    #0000 0000 0000 0000 0011 1110 0000 0000   - Tex5 coord frac
    #0000 0000 0000 0000 0100 0000 0000 0000   - Tex6 coord elements
    #0000 0000 0000 0011 1000 0000 0000 0000   - Tex6 coord format
    #0000 0000 0111 1100 0000 0000 0000 0000   - Tex6 coord frac
    #0000 0000 1000 0000 0000 0000 0000 0000   - Tex7 coord elements
    #0000 0111 0000 0000 0000 0000 0000 0000   - Tex7 coord format
    #1111 1000 0000 0000 0000 0000 0000 0000   - Tex7 coord frac
    return [(lo & 31), ((lo >> 5) & 1), ((lo >> 6) & 7), ((lo >> 9) & 31), ((lo >> 14) & 1),
            ((lo >> 15) & 7), ((lo >> 18) & 31), ((lo >> 23) & 1), ((lo >> 24) & 7), ((lo >> 27) & 31)]

#[
def CP_VCD_REG_LO(lo): # CP register
    #point fmt (17/32 bits used)
    #0000 0000 0000 0000 0000 0000 0000 0001     - Vert/Normal matrix (0)
    #0000 0000 0000 0000 0000 0000 0000 0010     - UV Matrix 0 (1)
    #0000 0000 0000 0000 0000 0000 0000 0100     - UV Matrix 1 (2)
    #0000 0000 0000 0000 0000 0000 0000 1000     - UV Matrix 2 (3)
    #0000 0000 0000 0000 0000 0000 0001 0000     - UV Matrix 3 (4)
    #0000 0000 0000 0000 0000 0000 0010 0000     - UV Matrix 4 (5)
    #0000 0000 0000 0000 0000 0000 0100 0000     - UV Matrix 5 (6)
    #0000 0000 0000 0000 0000 0000 1000 0000     - UV Matrix 6 (7)
    #0000 0000 0000 0000 0000 0001 0000 0000     - UV Matrix 7 (8)
    #0000 0000 0000 0000 0000 0110 0000 0000     - Vert format (9)
    #0000 0000 0000 0000 0001 1000 0000 0000     - Normal format (10)
    #0000 0000 0000 0000 0110 0000 0000 0000     - Color0 format (11)
    #0000 0000 0000 0001 1000 0000 0000 0000     - Color1 format (12)
    return [(lo & 1), ((lo >> 1) & 1), ((lo >> 2) & 1), ((lo >> 3) & 1), ((lo >> 4) & 1),
            ((lo >> 5) & 1), ((lo >> 6) & 1), ((lo >> 7) & 1), ((lo >> 8) & 1),
            ((lo >> 9) & 3), ((lo >> 11) & 3), ((lo >> 13) & 3), ((lo >> 15) & 3)]

def CP_VCD_REG_HI(hi):
    #texture inputs (16/32 bits used)
    #0000 0000 0000 0000 0000 0000 0000 0011     - UV0 format (0)
    #0000 0000 0000 0000 0000 0000 0000 1100     - UV1 format (1)
    #0000 0000 0000 0000 0000 0000 0011 0000     - UV2 format (2)
    #0000 0000 0000 0000 0000 0000 1100 0000     - UV3 format (3)
    #0000 0000 0000 0000 0000 0011 0000 0000     - UV4 format (4)
    #0000 0000 0000 0000 0000 1100 0000 0000     - UV5 format (5)
    #0000 0000 0000 0000 0011 0000 0000 0000     - UV6 format (6)
    #0000 0000 0000 0000 1100 0000 0000 0000     - UV7 format (7)
    return [(hi & 3), ((hi >> 2) & 3), ((hi >> 4) & 3), ((hi >> 6) & 3),\
    ((hi >> 8) & 3), ((hi >> 10) & 3), ((hi >> 12) & 3), ((hi >> 14) & 3)]
#]

def INVTXSPEC(host): #08 00 CP command
    # 0000 0000 0000 0000 0000 0011 0000 0000     - HOST_COLORS
    # 0000 0000 0000 0000 0000 1100 0000 0000     - HOST_NORMAL
    # 0000 0000 0000 0000 1111 0000 0000 0000     - HOST_TEXTURES (UVs)
    return [((host >> 8) & 3), ((host >> 10) & 3), ((host >> 12) & 15)]

def XFArrayFlags(flags): #XF register
    #XF commands (21/32 bits used)
    #0000 0000 0000 0000 0000 0000 0000 0001 Vert Mtx
    #0000 0000 0000 0000 0000 0000 0000 0010 UVMtx0
    #0000 0000 0000 0000 0000 0000 0000 0100 UVMtx1
    #0000 0000 0000 0000 0000 0000 0000 1000 UVMtx2
    #0000 0000 0000 0000 0000 0000 0001 0000 UVMtx3
    #0000 0000 0000 0000 0000 0000 0010 0000 UVMtx4
    #0000 0000 0000 0000 0000 0000 0100 0000 UVMtx5
    #0000 0000 0000 0000 0000 0000 1000 0000 UVMtx6
    #0000 0000 0000 0000 0000 0001 0000 0000 UVMtx7
    #0000 0000 0000 0000 0000 0010 0000 0000 Verts
    #0000 0000 0000 0000 0000 0100 0000 0000 Normals
    #0000 0000 0000 0000 0000 1000 0000 0000 Color0
    #0000 0000 0000 0000 0001 0000 0000 0000 Color1
    #0000 0000 0000 0000 0010 0000 0000 0000 UV0
    #0000 0000 0000 0000 0100 0000 0000 0000 UV1
    #0000 0000 0000 0000 1000 0000 0000 0000 UV2
    #0000 0000 0000 0001 0000 0000 0000 0000 UV3
    #0000 0000 0000 0010 0000 0000 0000 0000 UV4
    #0000 0000 0000 0100 0000 0000 0000 0000 UV5
    #0000 0000 0000 1000 0000 0000 0000 0000 UV6
    #0000 0000 0001 0000 0000 0000 0000 0000 UV7
    return [(flags & 1),
 ((flags >> 1) & 1), ((flags >> 2) & 1), ((flags >> 3) & 1), ((flags >> 4) & 1),
 ((flags >> 5) & 1), ((flags >> 6) & 1), ((flags >> 7) & 1), ((flags >> 8) & 1),
 ((flags >> 9) & 1), ((flags >>10) & 1), ((flags >>11) & 1), ((flags >>12) & 1),
 ((flags >>13) & 1), ((flags >>14) & 1), ((flags >>15) & 1), ((flags >>16) & 1),
 ((flags >>17) & 1), ((flags >>18) & 1), ((flags >>19) & 1), ((flags >>20) & 1)]

def XFNormalFormat(spec): #XF point spec
    #0000 0000 0000 0011   - Num colors
    #0000 0000 0000 1100   - Normal type (1 = normals, 2 = normals + binormals)
    #0000 0000 1111 0000   - Num channels
    return [(spec & 3), ((spec >> 2) & 3), ((spec >> 4) & 15)]

"""
        LoadBPReg = 0x61,
        LoadCPReg = 0x08,
        LoadXFReg = 0x10,

        //LoadIndex - 32bit data
        //0000 0000 0000 0000 0000 1111 1111 1111   - XF Memory address
        //0000 0000 0000 0000 1111 0000 0000 0000   - Length (reads length + 1 uint values into XF memory)
        //1111 1111 1111 1111 0000 0000 0000 0000   - Index (for matrices, this is node index)
        LoadIndexA = 0x20, //Position matrices (4 x 3)
        LoadIndexB = 0x28, //Normal matrices (3 x 3)
        LoadIndexC = 0x30, //Post matrices (4 x 4)
        LoadIndexD = 0x38, //Lights
        CmdCallDL = 0x40,
        CmdUnknownMetrics = 0x44,
        CmdInvlVC = 0x48,
        DrawQuads = 0x80,
        DrawTriangles = 0x90,
        DrawTriangleStrip = 0x98,
        DrawTriangleFan = 0xA0,
        DrawLines = 0xA8,
        DrawLineStrip = 0xB0,
        DrawPoints = 0xB8
        
    public enum XFMemoryAddr : ushort
    {
        PosMatrices = 0x0000,

        Size = 0x8000,
        Error = 0x1000,
        Diag = 0x1001,
        State0 = 0x1002,
        State1 = 0x1003,
        Clock = 0x1004,
        ClipDisable = 0x1005,
        SetGPMetric = 0x1006,

        VTXSpecs = 0x1008,
        SetNumChan = 0x1009,
        SetChan0AmbColor = 0x100A,
        SetChan1AmbColor = 0x100B,
        SetChan0MatColor = 0x100C,
        SetChan1MatColor = 0x100D,
        SetChan0Color = 0x100E,
        SetChan1Color = 0x100F,
        SetChan0Alpha = 0x1010,
        SetChan1Alpha = 0x1011,
        DualTex = 0x1012,
        SetMatrixIndA = 0x1018,
        SetMatrixIndB = 0x1019,
        SetViewport = 0x101A,
        SetZScale = 0x101C,
        SetZOffset = 0x101F,
        SetProjection = 0x1020,
        SetNumTexGens = 0x103F,
        SetTexMtxInfo = 0x1040,
        SetPosMtxInfo = 0x1050

    }
"""
def getCP_List(lo,hi): # CP register
    #point fmt "lo" (17/32 bits used)
    #0000 0000 0000 0000 0000 0000 0000 0001     - Vert/Normal matrix (0)
    #0000 0000 0000 0000 0000 0000 0000 0010     - UV Matrix 0 (1)
    #0000 0000 0000 0000 0000 0000 0000 0100     - UV Matrix 1 (2)
    #0000 0000 0000 0000 0000 0000 0000 1000     - UV Matrix 2 (3)
    #0000 0000 0000 0000 0000 0000 0001 0000     - UV Matrix 3 (4)
    #0000 0000 0000 0000 0000 0000 0010 0000     - UV Matrix 4 (5)
    #0000 0000 0000 0000 0000 0000 0100 0000     - UV Matrix 5 (6)
    #0000 0000 0000 0000 0000 0000 1000 0000     - UV Matrix 6 (7)
    #0000 0000 0000 0000 0000 0001 0000 0000     - UV Matrix 7 (8)
    #0000 0000 0000 0000 0000 0110 0000 0000     - Vert format (9)
    #0000 0000 0000 0000 0001 1000 0000 0000     - Normal format (10)
    #0000 0000 0000 0000 0110 0000 0000 0000     - Color0 format (11)
    #0000 0000 0000 0001 1000 0000 0000 0000     - Color1 format (12)

    #texture inputs "hi" (16/32 bits used)
    #0000 0000 0000 0000 0000 0000 0000 0011     - UV0 format (0)
    #0000 0000 0000 0000 0000 0000 0000 1100     - UV1 format (1)
    #0000 0000 0000 0000 0000 0000 0011 0000     - UV2 format (2)
    #0000 0000 0000 0000 0000 0000 1100 0000     - UV3 format (3)
    #0000 0000 0000 0000 0000 0011 0000 0000     - UV4 format (4)
    #0000 0000 0000 0000 0000 1100 0000 0000     - UV5 format (5)
    #0000 0000 0000 0000 0011 0000 0000 0000     - UV6 format (6)
    #0000 0000 0000 0000 1100 0000 0000 0000     - UV7 format (7)

    return [
    (lo & 1), ((lo >> 1) & 1), ((lo >> 2) & 1), ((lo >> 3) & 1), ((lo >> 4) & 1),
    ((lo >> 5) & 1), ((lo >> 6) & 1), ((lo >> 7) & 1), ((lo >> 8) & 1),
    ((lo >> 9) & 3), ((lo >> 11) & 3), ((lo >> 13) & 3), ((lo >> 15) & 3),
    (hi & 3), ((hi >> 2) & 3), ((hi >> 4) & 3), ((hi >> 6) & 3),
    ((hi >> 8) & 3), ((hi >> 10) & 3), ((hi >> 12) & 3), ((hi >> 14) & 3) ]


def getDisplayList( CP_List, offset, size ):
    
    def CP_Type(i): #face-point index/value formats
        if i == 0: return '', 0 #null
        if i == 1: return C.ru8(), 1 #direct data
        if i == 2: return C.ru8(), 1 #8bit index
        if i == 3: return C.ru16(), 2 #16bit index
        
    f.seek(offset,0)
    FacePoints = []
    Matrices = []
    Primitive_data = []
    
    position = 0
    while position < size:

        def matrix(primitive):
            Matrices.append(primitive.append(C.ru32()))
            return 4
        
        def geometry(primitive):
            Length = 2
            count = C.ru16()
            while count > 0:
                primitive[1].append(len(FacePoints))
                FacePoint = [] #build the facepoint:
                for Index in CP_List:
                    data, length = CP_Type(Index)
                    FacePoint.append(data)
                    Length += length
                FacePoints.append(FacePoint)
                count -= 1
            Primitive_data.append(primitive)
            return Length
            
        primitive = C.rhex(f.read(1))

        if primitive == '20': length = matrix([0])
        if primitive == '28': length = matrix([1])
        if primitive == '30': length = matrix([2])
        if primitive == '38': length = matrix([3])

        if primitive == '80': length = geometry([6,[]])
        if primitive == '90': length = geometry([3,[]])
        if primitive == '98': length = geometry([4,[]])
        if primitive == 'A0': length = geometry([5,[]])
        if primitive == 'A8': length = geometry([1,[]])
        if primitive == 'B0': length = geometry([2,[]])
        if primitive == 'B8': length = geometry([0,[]])
        
        position += (1 + length)
        #print str(size-position) + ' bytes left'
    
    return Matrices, FacePoints, Primitive_data
    """used to partially convert a Nintendo DisplayList to a TMP PrimitivesList
        (returns the matrices and facepoints for further work done in the plugin)"""

def DList2PList( FacePoints, Primitive_data ):
    PrimitivesList = [] #[P,[v,n,u,u,u,u,u,u,u,u]]
    Colors = [] #[V,[c,c]]
    Matrices = [] #[vn,u,u,u,u,u,u,u,u]
    CnvFacePoints = []
    
    for FacePoint in FacePoints:
        Matrices.append(FacePoint[0:9])
        Colors.append(FacePoint[11:13])
        CnvFacePoints.append(FacePoint[9:11]+FacePoint[13:22])
        
    for Primitive in Primitive_data:
        FPs = []
        for ID in Primitive[1]: FPs.append(CnvFacePoints[ID])
        PrimitivesList.append([Primitive[0],FPs])
        
    return PrimitivesList, Colors, Matrices
    """used to finish the conveC.rsion of a Nintendo DisplayList to a TMP PrimitivesList
        and also returns the matrix and color values in Nintendo FacePoints
        (merges the facepoints with the primitive data returned from 'getDisplayList')"""

def getNVector( D_Type, modifier, offset, size, ISvec3): 
    def DataType(): #decide the type of conversion to use
        if D_Type==0: return C.ru8() / pow(2.0,modifier),1
        if D_Type==1: return C.rs8() / pow(2.0,modifier),1
        if D_Type==2: return C.ru16() / pow(2.0,modifier),2
        if D_Type==3: return C.rs16() / pow(2.0,modifier),2
        if D_Type==4: return C.rf(),4
        
    f.seek(offset,0)

    VectorList = []
    position = 0
    while position < size:
        vector = []
        X,l1 = DataType()
        Y,l2 = DataType()
        position += (l1+l2)
        vector.append(X)
        vector.append(Y)
        if ISvec3:
            Z,l3 = DataType()
            position += l3
            vector.append(Z)
        VectorList.append(vector)

    return VectorList
    """used to read Nintendo Vector data and convert it to a TMP VectorList"""
